import superagent from 'superagent'
import fs from 'fs'
import path from 'path'

// 只负责爬取内容 爬完了怎么分析交给分析器去做 分析器拿回来 后保存数据
export interface Analyze {
  analyze: (html: string, filePath: string) => string
}
export default class Crowller {
  private filePath = path.resolve(__dirname, '../../data/course.json')

  private async getRawHtml() {
    const result = await superagent.get(this.url)
    return result.text
  }

  private writeFile(content: string) {
    fs.writeFileSync(this.filePath, content)
  }

  private async initSpideProcess() {
    const html = await this.getRawHtml()
    const fileContent = this.analyzer.analyze(html, this.filePath)
    this.writeFile(fileContent)
  }
  constructor(private url: string, private analyzer: Analyze) {
    this.initSpideProcess()
  }
}
